package com.ruoyi.web.controller.credit;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.CreditRoom;
import com.ruoyi.system.domain.CreditHotel;
import com.ruoyi.system.domain.CreditGoods;
import com.ruoyi.system.service.ICreditRoomService;
import com.ruoyi.system.service.ICreditHotelService;
import com.ruoyi.system.service.ICreditGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 信用商业服务Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/credit/business")
public class CreditBusinessController extends BaseController
{
    @Autowired
    private ICreditRoomService creditRoomService;
    
    @Autowired
    private ICreditHotelService creditHotelService;
    
    @Autowired
    private ICreditGoodsService creditGoodsService;

    /**
     * 查询房源信息列表
     */
    @PreAuthorize("@ss.hasPermi('credit:business:room:list')")
    @GetMapping("/room/list")
    public TableDataInfo list(CreditRoom creditRoom)
    {
        startPage();
        List<CreditRoom> list = creditRoomService.selectCreditRoomList(creditRoom);
        return getDataTable(list);
    }

    /**
     * 导出房源信息列表
     */
    @PreAuthorize("@ss.hasPermi('credit:business:room:export')")
    @Log(title = "房源信息", businessType = BusinessType.EXPORT)
    @PostMapping("/room/export")
    public void export(HttpServletResponse response, CreditRoom creditRoom)
    {
        List<CreditRoom> list = creditRoomService.selectCreditRoomList(creditRoom);
        ExcelUtil<CreditRoom> util = new ExcelUtil<CreditRoom>(CreditRoom.class);
        util.exportExcel(response, list, "房源信息数据");
    }

    /**
     * 获取房源信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('credit:business:room:query')")
    @GetMapping(value = "/room/{roomId}")
    public AjaxResult getInfo(@PathVariable("roomId") Long roomId)
    {
        return success(creditRoomService.selectCreditRoomByRoomId(roomId));
    }

    /**
     * 新增房源信息
     */
    @PreAuthorize("@ss.hasPermi('credit:business:room:add')")
    @Log(title = "房源信息", businessType = BusinessType.INSERT)
    @PostMapping("/room")
    public AjaxResult add(@RequestBody CreditRoom creditRoom)
    {
        return toAjax(creditRoomService.insertCreditRoom(creditRoom));
    }

    /**
     * 修改房源信息
     */
    @PreAuthorize("@ss.hasPermi('credit:business:room:edit')")
    @Log(title = "房源信息", businessType = BusinessType.UPDATE)
    @PutMapping("/room")
    public AjaxResult edit(@RequestBody CreditRoom creditRoom)
    {
        return toAjax(creditRoomService.updateCreditRoom(creditRoom));
    }

    /**
     * 删除房源信息
     */
    @PreAuthorize("@ss.hasPermi('credit:business:room:remove')")
    @Log(title = "房源信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/room/{roomIds}")
    public AjaxResult remove(@PathVariable Long[] roomIds)
    {
        return toAjax(creditRoomService.deleteCreditRoomByRoomIds(roomIds));
    }

    /**
     * 查询酒店信息列表
     */
    @PreAuthorize("@ss.hasPermi('credit:business:hotel:list')")
    @GetMapping("/hotel/list")
    public TableDataInfo hotelList(CreditHotel creditHotel)
    {
        startPage();
        List<CreditHotel> list = creditHotelService.selectCreditHotelList(creditHotel);
        return getDataTable(list);
    }

    /**
     * 导出酒店信息列表
     */
    @PreAuthorize("@ss.hasPermi('credit:business:hotel:export')")
    @Log(title = "酒店信息", businessType = BusinessType.EXPORT)
    @PostMapping("/hotel/export")
    public void exportHotel(HttpServletResponse response, CreditHotel creditHotel)
    {
        List<CreditHotel> list = creditHotelService.selectCreditHotelList(creditHotel);
        ExcelUtil<CreditHotel> util = new ExcelUtil<CreditHotel>(CreditHotel.class);
        util.exportExcel(response, list, "酒店信息数据");
    }

    /**
     * 获取酒店信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('credit:business:hotel:query')")
    @GetMapping(value = "/hotel/{hotelId}")
    public AjaxResult getHotelInfo(@PathVariable("hotelId") Long hotelId)
    {
        return success(creditHotelService.selectCreditHotelByHotelId(hotelId));
    }

    /**
     * 新增酒店信息
     */
    @PreAuthorize("@ss.hasPermi('credit:business:hotel:add')")
    @Log(title = "酒店信息", businessType = BusinessType.INSERT)
    @PostMapping("/hotel")
    public AjaxResult addHotel(@RequestBody CreditHotel creditHotel)
    {
        return toAjax(creditHotelService.insertCreditHotel(creditHotel));
    }

    /**
     * 修改酒店信息
     */
    @PreAuthorize("@ss.hasPermi('credit:business:hotel:edit')")
    @Log(title = "酒店信息", businessType = BusinessType.UPDATE)
    @PutMapping("/hotel")
    public AjaxResult editHotel(@RequestBody CreditHotel creditHotel)
    {
        return toAjax(creditHotelService.updateCreditHotel(creditHotel));
    }

    /**
     * 删除酒店信息
     */
    @PreAuthorize("@ss.hasPermi('credit:business:hotel:remove')")
    @Log(title = "酒店信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/hotel/{hotelIds}")
    public AjaxResult removeHotel(@PathVariable Long[] hotelIds)
    {
        return toAjax(creditHotelService.deleteCreditHotelByHotelIds(hotelIds));
    }

    /**
     * 查询商品信息列表
     */
    @PreAuthorize("@ss.hasPermi('credit:business:goods:list')")
    @GetMapping("/goods/list")
    public TableDataInfo goodsList(CreditGoods creditGoods)
    {
        startPage();
        List<CreditGoods> list = creditGoodsService.selectCreditGoodsList(creditGoods);
        return getDataTable(list);
    }

    /**
     * 导出商品信息列表
     */
    @PreAuthorize("@ss.hasPermi('credit:business:goods:export')")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/goods/export")
    public void exportGoods(HttpServletResponse response, CreditGoods creditGoods)
    {
        List<CreditGoods> list = creditGoodsService.selectCreditGoodsList(creditGoods);
        ExcelUtil<CreditGoods> util = new ExcelUtil<CreditGoods>(CreditGoods.class);
        util.exportExcel(response, list, "商品信息数据");
    }

    /**
     * 获取商品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('credit:business:goods:query')")
    @GetMapping(value = "/goods/{goodsId}")
    public AjaxResult getGoodsInfo(@PathVariable("goodsId") Long goodsId)
    {
        return success(creditGoodsService.selectCreditGoodsByGoodsId(goodsId));
    }

    /**
     * 新增商品信息
     */
    @PreAuthorize("@ss.hasPermi('credit:business:goods:add')")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping("/goods")
    public AjaxResult addGoods(@RequestBody CreditGoods creditGoods)
    {
        return toAjax(creditGoodsService.insertCreditGoods(creditGoods));
    }

    /**
     * 修改商品信息
     */
    @PreAuthorize("@ss.hasPermi('credit:business:goods:edit')")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PutMapping("/goods")
    public AjaxResult editGoods(@RequestBody CreditGoods creditGoods)
    {
        return toAjax(creditGoodsService.updateCreditGoods(creditGoods));
    }

    /**
     * 删除商品信息
     */
    @PreAuthorize("@ss.hasPermi('credit:business:goods:remove')")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/goods/{goodsIds}")
    public AjaxResult removeGoods(@PathVariable Long[] goodsIds)
    {
        return toAjax(creditGoodsService.deleteCreditGoodsByGoodsIds(goodsIds));
    }

    // ============================================================================
    // 无权限接口 - 供前端调用
    // ============================================================================

    /**
     * 获取可用房源列表（无权限）
     */
    @GetMapping("/room/available")
    public AjaxResult getAvailableRooms()
    {
        try {
            List<CreditRoom> rooms = creditRoomService.selectAvailableCreditRoomList();
            return success(rooms);
        } catch (Exception e) {
            return error("获取房源信息失败：" + e.getMessage());
        }
    }

    /**
     * 获取可用酒店列表（无权限）
     */
    @GetMapping("/hotel/available")
    public AjaxResult getAvailableHotels()
    {
        try {
            List<CreditHotel> hotels = creditHotelService.selectAvailableCreditHotelList();
            return success(hotels);
        } catch (Exception e) {
            return error("获取酒店信息失败：" + e.getMessage());
        }
    }

    /**
     * 获取可用商品列表（无权限）
     */
    @GetMapping("/goods/available")
    public AjaxResult getAvailableGoods()
    {
        try {
            List<CreditGoods> goods = creditGoodsService.selectAvailableCreditGoodsList();
            return success(goods);
        } catch (Exception e) {
            return error("获取商品信息失败：" + e.getMessage());
        }
    }

    /**
     * 获取推荐商品列表（无权限）
     */
    @GetMapping("/goods/recommended")
    public AjaxResult getRecommendedGoods()
    {
        try {
            List<CreditGoods> goods = creditGoodsService.selectRecommendedCreditGoodsList();
            return success(goods);
        } catch (Exception e) {
            return error("获取推荐商品失败：" + e.getMessage());
        }
    }

    /**
     * 获取房源详情（无权限）
     */
    @GetMapping("/room/detail/{roomId}")
    public AjaxResult getRoomDetail(@PathVariable("roomId") Long roomId)
    {
        try {
            CreditRoom room = creditRoomService.selectCreditRoomByRoomId(roomId);
            if (room == null) {
                return error("房源不存在");
            }
            return success(room);
        } catch (Exception e) {
            return error("获取房源详情失败：" + e.getMessage());
        }
    }

    /**
     * 获取酒店详情（无权限）
     */
    @GetMapping("/hotel/detail/{hotelId}")
    public AjaxResult getHotelDetail(@PathVariable("hotelId") Long hotelId)
    {
        try {
            CreditHotel hotel = creditHotelService.selectCreditHotelByHotelId(hotelId);
            if (hotel == null) {
                return error("酒店不存在");
            }
            return success(hotel);
        } catch (Exception e) {
            return error("获取酒店详情失败：" + e.getMessage());
        }
    }

    /**
     * 获取商品详情（无权限）
     */
    @GetMapping("/goods/detail/{goodsId}")
    public AjaxResult getGoodsDetail(@PathVariable("goodsId") Long goodsId)
    {
        try {
            CreditGoods goods = creditGoodsService.selectCreditGoodsByGoodsId(goodsId);
            if (goods == null) {
                return error("商品不存在");
            }
            return success(goods);
        } catch (Exception e) {
            return error("获取商品详情失败：" + e.getMessage());
        }
    }
} 